﻿@using DormMS.Model.Enum
@{
    Layout="";
}

<style>
    .ngdialog.ngdialog-theme-plain .ngdialog-content {
        width: 60% !important;
    }
</style>

<h3>奖项编辑</h3>
<div class="panel panel-default" ng-controller="DrawRewardCtrl" ng-class="{ 'whirl standard': isLoading,'': isLoading }">
    <div class="panel-heading">
        <div class="form-inline ng-pristine ng-valid">
            <div class="form-group" style="width:100%;">
                <button type="submit" class="btn btn-labeled btn-success" ng-click="editDrawReward(0)">
                    <span class="btn-label">
                        <em class="fa fa-plus"></em>
                    </span>新增
                </button>
            </div>
        </div>
    </div>
    <div class="panel-body">
        <div class="table-responsive">
            <table ng-table="tableParams" class="table table-striped table-bproducted table-hover">
                <colgroup>
                    <col style="width: 15%" />
                    <col style="width: 10%" />
                    <col style="width: 10%" />
                    <col style="width: 20%" />
                    <col style="width: 10%" />
                    <col style="width: 10%" />
                    <col style="width: 10%" />
                    <col style="width: 15%" />
                </colgroup>
                <tbody>
                    <tr ng-repeat="item in $data">
                        <td data-title="'奖项名'" class="text-left">{{item.RewardName}}</td>
                        <td data-title="'奖品类型'" sortable="'RewardTypeStr'" class="text-center">{{item.RewardTypeStr}}</td>
                        <td data-title="'派发总数'" sortable="'Count'" class="text-center">{{item.Count}}</td>
                        <td data-title="'奖品名'" class="text-center">{{item.ProductName}}</td>
                        <td data-title="'奖励积分数'" sortable class="text-center">{{item.Points}}</td>
                        <td data-title="'中奖率'" class="text-center">{{item.WinRate}}</td>
                        <td data-title="'添加时间'" sortable="'AddTime'" class="text-center">{{item.AddTime}}</td>
                        <td data-title="'操作'" nowrap="nowrap" class="text-center">
                            <button type="button" ng-click="editDrawReward(item.Id)" class="btn btn-sm btn-info" title="编辑">
                                <em class="fa fa-edit"></em>
                            </button>
                            <button ng-click="deleteDraw(item.Id)" type="button" class="btn btn-sm btn-danger" title="删除">
                                <em class="fa fa-trash-o"></em>
                            </button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div class="panel-footer text-right">
        <button type="button" class="btn btn-warning" ng-click="cancel()">返回列表</button>
    </div>
    <script type="text/ng-template" id="modalDialogId">
        <div class="ngdialog-message text-center">
            <h3>确定要删除吗?</h3>
            <p>删除后不可恢复!</p>
            <br />
            <button type="button" ng-click="confirm()" class="btn btn-primary mr">确认</button>
            <button type="button" ng-click="closeThisDialog()" class="btn btn-default">取消</button>
        </div>
    </script>
    @*<script type="text/ng-template" id="editReward">
        <div class="ngdialog-message">
            <div class="row">
                <div class="col-md-12">
                    <form class="form-horizontal">
                        <div class="panel panel-default">
                            <div class="panel-body">
                                <fieldset class="b0">
                                    <legend>{{EditedReward.Id==0||EditedReward.Id==null?"新增":"修改"}}奖项</legend>
                                </fieldset>
                                <fieldset>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">奖项名称</label>
                                        <div class="col-sm-6">
                                            <input type="text" ng-model="EditedReward.RewardName" class="form-control" maxlength="50" />
                                        </div>
                                        <div class="col-sm-3">
                                            <h4 class="text-danger" style="color:red">*</h4>
                                        </div>
                                    </div>
                                </fieldset>
                                <fieldset>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">奖项类型</label>
                                        <div class="col-sm-6">
                                            <select ng-model="EditedReward.RewardType" class="form-control" ng-change="changeType()">
                                                <option value="1">实体商品</option>
                                                <option value="2">优惠券</option>
                                                <option value="3">积分奖励</option>
                                            </select>
                                        </div>
                                        <div class="col-sm-3">
                                            <h4 class="text-danger" style="color:red">*</h4>
                                        </div>
                                    </div>
                                </fieldset>
                                <fieldset>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">奖品</label>
                                        <div class="col-sm-6">
                                            <select ng-model="EditedReward.ProductSecpId" class="form-control" ng-options="m.Id as m.Name for m in ProductList">
                                                <option value="">请选择奖品</option>                                              
                                            </select>
                                        </div>
                                        <div class="col-sm-3">
                                            <h4 class="text-danger" style="color:red">*</h4>
                                        </div>
                                    </div>
                                </fieldset>
                                <fieldset>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">奖品派发总数</label>
                                        <div class="col-sm-6">
                                            <input type="number" ng-model="EditedReward.Count" class="form-control"/>
                                        </div>
                                        <div class="col-sm-3">
                                            <h4 class="text-danger" style="color:red">*</h4>
                                        </div>
                                    </div>
                                </fieldset>
                                <fieldset ng-show="EditedReward.RewardType==@((int)RewardType.奖励积分)">
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">奖励积分数</label>
                                        <div class="col-sm-6">
                                            <input type="number" ng-model="EditedReward.Points" class="form-control" />
                                        </div>
                                        <div class="col-sm-3">
                                            <h4 class="text-danger" style="color:red">*</h4>
                                        </div>
                                    </div>
                                </fieldset>
                                <fieldset>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">中奖几率（百分数）</label>
                                        <div class="col-sm-6">
                                            <input type="number" ng-model="EditedReward.WinRate" class="form-control" />
                                        </div>
                                        <div class="col-sm-3">
                                            <h4 class="text-danger" style="color:red">*</h4>
                                        </div>
                                    </div>
                                </fieldset>            
                            </div>
                            <div class="panel-footer text-right">
                                <button type="button" class="btn btn-default" ng-click="closeDialog()">
                                    关闭
                                </button>
                                <button type="button" class="btn btn-default" ng-click="saveReward()">
                                    保存
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </script>*@
</div>


<script>
    App.controller('DrawRewardCtrl', ['$scope', '$resource', '$state', '$route', 'ngDialog', '$http', 'ngTableParams', 'ngTableDataService', 'Notify', '$modal', "$filter", "$timeout", "$stateParams",
        function ($scope, $resource, $state, $route, ngDialog, $http, ngTableParams, ngTableDataService, Notify, $modal, $filter, $timeout, $stateParams) {
            'use strict';
            $scope.IsLoading = true;
            $scope.EditedReward = {};
            $scope.ProductList = [];
            $scope.DrawId = $stateParams.Id;
            $scope.tableParams = new ngTableParams({
                page: 1,
                count: 10,
                sorting: {
                    //Id: 'desc'     // 初始化排序
                }
            }, {
                getData: function ($defer, params) {
                        Skip: (params.page() - 1) * params.count(),
                    $scope.isLoading = true;
                    var requestParams = {
                        Take: params.count(),
                        DrawId: $stateParams.Id
                    };
                    $http.post('/Draw/DrawRewardList', ngTableDataService.getServerSideParams(params.orderBy().toString(), params, requestParams))
                        .success(function (data, status) {

                            params.total(data.Total);
                            $defer.resolve(data.Results);

                            //记录查询参数，后续返回使用
                            $scope.urlParams = {
                                page: params.page(),
                                Keywords: $scope.Keywords,
                                //FirstCategoryId: $scope.FirstCategoryId,
                                //SecondCategoryId: $scope.SecondCategoryId,
                                IsDisplay: $scope.IsDisplay
                            }
                            $scope.isLoading = false;
                        });
                }
            });

            //返回列表页
            $scope.cancel = function () {
                $state.go("app.draw");
            }

            //获取产品列表
            $http.post("/Draw/GetActProducts").then(function (response) {
                $scope.AllProduct = response.data;
            })

            //获取优惠券列表
            $http.post('/Draw/GetActCoupons').then(function (response) {
                $scope.AllCoupons = response.data;
            })

            @*$scope.changeType = function () {
                console.log($scope.EditedReward.RewardType);
                if($scope.EditedReward.RewardType== '@((int)RewardType.实体商品)'){
                    $scope.ProductList = $scope.AllProduct;
                }
                else if ($scope.EditedReward.RewardType == '@((int)RewardType.优惠券)') {
                    $scope.ProductList = $scope.AllCoupons;
                }
                else if ($scope.EditedReward.RewardType == '@((int)RewardType.奖励积分)') {
                    $scope.ProductList = [{ Id: 0, Name: "积分奖励" }];
                }
            }*@

            //新增或删除
            $scope.editDrawReward = function (id) {
                $state.go('app.drawRewardEditNew', { Id: id, DrawId:$scope.DrawId });
            }

            //新增或删除
            //$scope.editDrawReward = function (id) {
            //    //新增
            //    if (id <= 0) {
            //        $scope.EditedReward.Id = 0;
            //        $scope.EditedReward.DrawId = $stateParams.Id;
            //    }
            //    //编辑
            //    else {
            //        $http.post('/Draw/GetRewardInfo', { Id: id }).then(function (response) {
            //            $scope.EditedReward = response.data;
            //            $scope.EditedReward.DrawId = $stateParams.Id;
            //            $scope.changeType();
            //        })
            //    }
            //    ngDialog.openConfirm({
            //        template: 'editReward',
            //        className: 'ngdialog-theme-plain',
            //        scope: $scope,
            //        preCloseCallback: function () {
            //            return true;
            //        }
            //    }).then(function (value) {
            //    }, function (reason) {
            //    }
            //    );
            //}

            //关闭对话框
            //$scope.closeDialog = function () {
            //    ngDialog.closeAll();
            //}
            @*//保存
            $scope.saveReward = function () {
                if ($scope.EditedReward.RewardName == null || $scope.EditedReward.RewardName.length <= 0) {
                    Notify.alert('请输入奖项名称!', { status: 'warning', timeout: 1000 });
                    $scope.IsLoading = false;
                    return false;
                }
                if ($scope.EditedReward.Count <= 0) {
                    Notify.alert('请输入正确的库存数量!', { status: 'warning', timeout: 1000 });
                    $scope.IsLoading = false;
                    return false;
                }
                if ($scope.EditedReward.RewardType=='@((int)RewardType.奖励积分)'&&$scope.EditedReward.Points <= 0) {
                    Notify.alert('请输入正确的积分奖励!', { status: 'warning', timeout: 1000 });
                    $scope.IsLoading = false;
                    return false;
                }
                if ($scope.EditedReward.WinRate < 0 || $scope.EditedReward.WinRate > 100) {
                    Notify.alert('中奖几率必须在0~100之间', { status: 'warning', timeout: 1000 });
                    $scope.IsLoading = false;
                    return false;
                }
                if (($scope.EditedReward.RewardType == 1 || $scope.EditedReward.RewardType == 2) && $scope.EditedReward.ProductSecpId <= 0) {
                    Notify.alert('请选择奖品', { status: 'warning', timeout: 1000 });
                    $scope.IsLoading = false;
                    return false;
                }


                $http.post('/Draw/SaveReward', { reward: $scope.EditedReward }).then(function (response) {
                    if (response.data.isSuccess) {
                        Notify.alert('保存成功!', { status: 'success', timeout: 1000 });
                        $scope.EditedReward = {};
                        $scope.tableParams.reload();
                        $scope.closeDialog();
                    }
                    else {
                        Notify.alert(response.data.msg, { status: 'warning', timeout: 1000 });
                    }
                })
            }*@

            $scope.deleteDraw = function (id) {
                ngDialog.openConfirm({
                    template: 'modalDialogId',
                    className: 'ngdialog-theme-default'
                }).then(function (value) {
                    $http.post("/Draw/DeleteReward", { id: id }).then(function (returnData) {
                        if (returnData.data.isSuccess) {
                            $scope.tableParams.reload();
                            Notify.alert(returnData.data.msg, { status: 'success', timeout: 1000 });
                        } else {
                            Notify.alert(returnData.data.msg, { status: 'warning', timeout: 1000 });
                        }
                    });
                }, function (reason) {
                }
            );
            }
        }
    ])
</script>


